/*用户功能菜单*/
#include "head.h"

int domenu()            //用户功能菜单
{
    int i = 0;
    printf("--------请选择服务功能--------\n");
    printf("\t  1.下载文件\n");
    printf("\t  2.上传文件\n");
    printf("\t  3.退出程序\n");
    printf("\t  4.查看目录\n");
    printf("\t  9.账号管理\n");
    printf("-----------------------------\n");
    scanf("%d", &i);
    return i;
}

/*从服务器下载文件*/
void get_file(int sockfd)
{
    int ret = 0;
    int file_num = 0;
    int file_size = 0;
    int recv_size = 0;
    int recv_size_num = 0;
    char file[42] = {0};
    char buf[1024] = {0};
    char down_file[32] = {0};
    char file_size_str[32] = {0};

    scanf("%s", down_file);
    ret = send(sockfd, down_file, strlen(down_file) + 1, 0);
    if (ret < 0)
    {
        perror("send");
        return;
    }

    ret = recv(sockfd, file_size_str, sizeof(file_size_str) - 1, 0); // 接收要下载的文件字数
    if (ret < 0)
    {
        perror("recv");
        return;
    }
    printf("本次要下载的文件字数：%s\n", file_size_str);
    file_size = atoi(file_size_str);

    sprintf(file, "./file/%s", down_file);

    FILE *fp;

    if ((fp = fopen(file, "wb")) == NULL)
    {
        perror("fopen");
        return;
    }

    file_num = file_size;

    ret = send(sockfd, "111111", 7, 0); // 给服务器发送消息做好准备
    if (ret < 0)
    {
        perror("send");
        return;
    }

    while (file_num > 0)
    {
        int recv_how_size = file_num < 1024 ? file_num : 1024;
        recv_size = recv(sockfd, buf, recv_how_size, 0);
        if (recv_size < 0)
        {
            perror("recv");
            return;
        }
        fwrite(buf, 1, recv_size, fp);
        file_num -= recv_size;

        recv_size_num += recv_size;
        printf("had receive %d bytes data.\n", recv_size_num);
    }
    fclose(fp);
    printf("下载文件完成\n");
}

/*向服务器上传文件*/
void set_file(int sockfd)
{
    int ret = 0;
    int read_size = 0;
    int read_size_sum = 0;
    char buffer[1024];
    char file[42] = {0};
    char action[7] = {0};
    char file_size_str[32]; // 将文件字数发给服务器
    char set_file[32] = {0};

    scanf("%s", set_file);
    sprintf(file, "./file/%s", set_file);
    ret = send(sockfd, set_file, strlen(set_file) + 1, 0);
    if (ret < 0)
    {
        perror("send");
        return;
    }

    FILE *fp;
    sprintf(file, "./file/%s", set_file);
    fp = fopen(file, "rb");
    fseek(fp, 0, SEEK_END);
    int file_size = ftell(fp); // 计算文件字数
    fseek(fp, 0, SEEK_SET);

    sprintf(file_size_str, "%d", file_size); // 数字转字符串
    ret = send(sockfd, file_size_str, strlen(file_size_str) + 1, 0);
    if (ret < 0)
    {
        perror("send");
        return;
    }

    if (-1 == recv(sockfd, action, sizeof(action), 0)) // 接收服务器已经准备
    {
        perror("recv");
        return;
    }

    while ((read_size = fread(buffer, 1, 1024, fp)) > 0)
    {
        if (send(sockfd, buffer, read_size, 0) != read_size)
        {
            perror("send");
            return;
        }
        read_size_sum += read_size;
        printf("had send %d bytes data.\n", read_size_sum);
    }
    printf("上传中......\n");
    sleep(5);
    fclose(fp);
    printf("发送完成——————\n");
    return;
}

/*修改密码与注销模块*/
int user_modfiy(int sockfd)
{
    int i = 0;
    char buf[5];
    char buf_1[33];
    char buf_2[16];
    char buf_3[25];
    printf("1.注销账号\n");
    printf("2.修改密码\n");
    scanf("%d", &i);
    switch (i)
    {
    case 1:
    {
        sprintf(buf, "%d", i);
        int ret = send(sockfd, buf, sizeof(i), 0); // 发送功能请求
        if (ret < 0)
        {
            perror("send");
            return -1;
        }
        char buf_1[32] = {0};
        ret = recv(sockfd, buf_1, sizeof(buf_1) - 1, 0); // 账号注销成功否
        if (ret < 0)
        {
            perror("recv");
            return -1;
        }
        printf("%s\n", buf_1);
        if (strcmp(buf_1, "注销账号成功") == 0)
        {
            return 1;
        }
    }
    break;
    case 2:
    {
        sprintf(buf, "%d", i);
        int ret = send(sockfd, buf, sizeof(i), 0);
        if (ret < 0)
        {
            perror("send");
            return -1;
        }
        ret = recv(sockfd, buf_1, sizeof(buf_1) - 1, 0); // 收到“服务器发来的要修改为的密码”
        if (ret < 0)
        {
            perror("recv");
            return -1;
        }
        printf("%s\n", buf_1);
        scanf("%s", buf_2);
        ret = send(sockfd, buf_2, sizeof(buf_2), 0);
        if (ret < 0)
        {
            perror("send");
            return -1;
        }
        ret = recv(sockfd, buf_3, sizeof(buf_3), 0); // 密码修改成功否
        if (ret < 0)
        {
            perror("recv");
            return -1;
        }
        printf("%s\n", buf_3);
        if (strcmp(buf_2, "修改密码成功") == 0)
        {
            return 15;
        }
    }
    break;
    }
    return 0;
}


void recv_dir(int sockfd)       //发送文件信息
{
    dir info;
    if (recv(sockfd, &info, sizeof(dir), 0) < 0)
    {
        perror("recv");
        return;
    }
    printf("%-12s\t", info.file_name);
    printf("%d\t", info.file_sort);
    switch (info.file_root)
    {
    case 1:
        printf("--x\n");
        break;
    case 2:
        printf("-w-\n");
        break;
    case 3:
        printf("-wx\n");
        break;
    case 5:
        printf("r-x\n");
        break;
    case 6:
        printf("rw-\n");
        break;
    case 7:
        printf("rwx\n");
        break;
    };
}


/*查看目录模块*/
void look_dir(int sockfd)
{
    int num = 0;
    int ret = 0;
    char file_num[5] = {0};

    ret = send(sockfd, "客户端请求目录", 22, 0);
    if (ret < 0)
    {
        perror("send");
        return;
    }

    ret = recv(sockfd, file_num, sizeof(file_num), 0); // 接收文件数
    if (ret < 0)
    {
        perror("recv");
        return;
    }
    printf("要接收的文件名数:%s\n", file_num);
    num = atoi(file_num);

    ret = send(sockfd, "请求发送", 14, 0); // 向服务器发送已经准备接收
    if (ret < 0)
    {
        perror("send");
        return;
    }

    printf("加载目录中....\n");
    for (int i = 0; i < num; i++)
    {
        recv_dir(sockfd);
    }
    printf("接收完成\n");
    return;
}